package com.jichangxiu.server.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.common.exception.ServiceException;
import com.jichangxiu.common.utils.TokenUtils;
import com.jichangxiu.framework.mybatis.BaseServiceImpl;
import com.jichangxiu.server.platform.entity.dto.PlatformDictDataQueryDto;
import com.jichangxiu.server.platform.entity.map.PlatformDictDataBasic;
import com.jichangxiu.server.platform.entity.po.PlatformDictData;
import com.jichangxiu.server.platform.entity.vo.PlatformDictDataAddVo;
import com.jichangxiu.server.platform.entity.vo.PlatformDictDataEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformDictDataQueryVo;
import com.jichangxiu.server.platform.mapper.PlatformDictDataMapper;
import com.jichangxiu.server.platform.service.PlatformDictDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * PlatformDictDataServiceImpl:
 *
 * @author Jcx
 * @create 2023-03-08 21:17
 */
@Service
public class PlatformDictDataServiceImpl extends BaseServiceImpl<PlatformDictDataMapper, PlatformDictData> implements PlatformDictDataService {

    @Resource
    private TokenUtils tokenUtils;

    @Resource
    private PlatformDictDataMapper platformDictDataMapper;

    /**
     * 新增字典值
     *
     * @param platformDictDataAddVo 字典值视图对象
     * @return 是否成功
     */
    @Override
    public Boolean add(PlatformDictDataAddVo platformDictDataAddVo) {
        if (ObjectUtil.isEmpty(platformDictDataAddVo)) {
            throw ServiceException.build("字典值对象无效");
        }

        PlatformDictData platformDictData = PlatformDictDataBasic.INSTANCE.toPlatformDictData(platformDictDataAddVo);
        if (ObjectUtil.isEmpty(platformDictData)) {
            throw ServiceException.build("字典值对象无效");
        }

        // type 必须唯一
        PlatformDictData dbPlatformDictDataByLabel = getOne(new LambdaQueryWrapper<PlatformDictData>().eq(PlatformDictData::getDataLabel, platformDictData.getDataLabel()));
        if (ObjectUtil.isNotEmpty(dbPlatformDictDataByLabel)) {
            throw ServiceException.build(platformDictData.getDictCode() + "该字典类型的字典值Label重复");
        }

        if (platformDictData.getIsDefault()) {
            List<PlatformDictData> platformDictDataList = list(new LambdaQueryWrapper<PlatformDictData>().in(PlatformDictData::getDictCode, platformDictData.getDictCode()));
            if (ObjectUtil.isNotEmpty(platformDictDataList)) {
                platformDictDataList.forEach(dbPlatformDictData -> {
                    if (dbPlatformDictData.getIsDefault()) {
                        throw ServiceException.build(platformDictData.getDictCode() + "该字典类型的字典值已经存在一个默认值了");
                    }
                });
            }
        }

        platformDictData.setTenantId(tokenUtils.getTenantId());
        return save(platformDictData);
    }

    /**
     * 多选级联删除字典值
     *
     * @param dataIdList 字典值ID列表
     * @return 是否成功
     */
    @Override
    public Boolean del(List<String> dataIdList) {
        List<PlatformDictData> platformDictDataList = list(new LambdaQueryWrapper<PlatformDictData>().in(PlatformDictData::getDataId, dataIdList));
        if (ObjectUtil.isEmpty(platformDictDataList)) {
            throw ServiceException.build("数据库中不存在需要删除的记录");
        }
        return removeBatchByIds(dataIdList);
    }

    /**
     * 修改字典值信息
     *
     * @param platformDictDataEditVo 字典值视图对象
     * @return 是否成功
     */
    @Override
    public Boolean edit(PlatformDictDataEditVo platformDictDataEditVo) {
        if (ObjectUtil.isEmpty(platformDictDataEditVo)) {
            throw ServiceException.build("字典值对象无效");
        }

        PlatformDictData platformDictData = PlatformDictDataBasic.INSTANCE.toPlatformDictData(platformDictDataEditVo);
        if (ObjectUtil.isEmpty(platformDictData)) {
            throw ServiceException.build("字典值对象无效");
        }

        PlatformDictData dbPlatformDictData = getOne(new LambdaQueryWrapper<PlatformDictData>().eq(PlatformDictData::getDataId, platformDictDataEditVo.getDataId()));
        if (ObjectUtil.isEmpty(dbPlatformDictData)) {
            throw ServiceException.build(platformDictData.getDictCode() + "该字典值在数据库中不存在");
        }

        if (!platformDictData.getDictId().equals(dbPlatformDictData.getDictId()) || !platformDictData.getDictCode().equals(dbPlatformDictData.getDictCode())) {
            throw ServiceException.build(platformDictData.getDictCode() + "该字典值不能修改字典类型ID和Code");
        }

        if (!platformDictData.getDataLabel().equals(dbPlatformDictData.getDataLabel())) {
            PlatformDictData dbPlatformDictDataByLabel = getOne(new LambdaQueryWrapper<PlatformDictData>().eq(PlatformDictData::getDataLabel, platformDictData.getDataLabel()));
            if (ObjectUtil.isNotEmpty(dbPlatformDictDataByLabel)) {
                throw ServiceException.build(platformDictData.getDictCode() + "该字典类型的字典值Label重复");
            }
        }

        if (platformDictData.getIsDefault()) {
            List<PlatformDictData> platformDictDataList = list(new LambdaQueryWrapper<PlatformDictData>().in(PlatformDictData::getDictCode, platformDictData.getDictCode()));
            if (ObjectUtil.isNotEmpty(platformDictDataList)) {
                platformDictDataList.forEach(dbPlatformDictDa -> {
                    if (dbPlatformDictDa.getIsDefault()) {
                        throw ServiceException.build(platformDictData.getDictCode() + "该字典类型的字典值已经存在一个默认值了");
                    }
                });
            }
        }

        BeanUtil.copyProperties(platformDictData, dbPlatformDictData, true);
        return updateById(dbPlatformDictData);
    }

    /**
     * 根据ID获取字典值详情
     *
     * @param dataId 字典值ID
     * @return 字典值详情
     */
    @Override
    public PlatformDictDataQueryDto info(String dataId) {
        PlatformDictData platformDictData = getOne(new LambdaQueryWrapper<PlatformDictData>().eq(PlatformDictData::getDataId, dataId));
        return PlatformDictDataBasic.INSTANCE.toPlatformDictDataQueryDto(platformDictData);
    }

    /**
     * 分页查询字典值列表
     *
     * @param platformDictDataQueryVo 字典值视图对象
     * @return 分页字典值列表
     */
    @Override
    public PageData<PlatformDictDataQueryDto> pageList(PlatformDictDataQueryVo platformDictDataQueryVo) {
        Page<PlatformDictData> page = startPage();
        List<PlatformDictData> platformDictDataList = platformDictDataMapper.pageList(PlatformDictDataBasic.INSTANCE.toPlatformDictData(platformDictDataQueryVo), page);
        List<PlatformDictDataQueryDto> platformDictDataQueryDtoList = PlatformDictDataBasic.INSTANCE.toPlatformDictDataQueryDtoList(platformDictDataList);
        return PageData.build(new Page<>(page.getCurrent(), page.getSize(), page.getTotal()), platformDictDataQueryDtoList);
    }

}
